#include "TimeStamp.h"
#include "TimeStampArray.h"

#include "StringArray.h"
#include "StringValue.h"

#include <iomanip>
#include <sstream>
#include <ctime>
#include <fstream>
#include <cstdint> // 包含int64_t
#include <array>


std::string convert_to_timestamp( int64_t sec_since_1904, uint64_t ns )
{
    unsigned long long max_ull = ULLONG_MAX;
    double ns1 =  ns / max_ull * 1.0;
    double total_seconds = sec_since_1904 + ns1;
    int64_t days = static_cast<int>( total_seconds / 86400 );
    int64_t seconds_in_day = static_cast<int>( total_seconds - days * 86400.0 );

    int year = 1904 + static_cast<int>( days / 365.25 );
//    int day_of_year = static_cast<int>( days % 365.25 ) + 1;
    int month = ( days % 365 ) / 30 + 1;
    int day_of_month = ( days % 365 ) % 30 + 1;

    int hour = seconds_in_day / 3600;
    int minute = ( seconds_in_day % 3600 ) / 60;
    int second = seconds_in_day % 60;

    // 构造时间字符串
    std::ostringstream oss;
    oss << std::setw( 4 ) << std::setfill( '0' ) << year
        << "-" << std::setw( 2 ) << std::setfill( '0' ) << month // 假设月份为1月（不准确）
        << "-" << std::setw( 2 ) << std::setfill( '0' ) << day_of_month // 使用天数代替具体日期（不准确）
        << " " << std::setw( 2 ) << std::setfill( '0' ) << hour
        << ":" << std::setw( 2 ) << std::setfill( '0' ) << minute
        << ":" << std::setw( 2 ) << std::setfill( '0' ) << second
        << "." << std::setw( 9 ) << std::setfill( '0' ) << ns;

    return oss.str();
}

// 函数用于反转字节序（假设是8字节）
uint64_t reverseBytes( const uint8_t* bytes )
{
    uint64_t value = 0;
    for ( int i = 0; i < 8; ++i )
    {
        value = ( value << 8 ) | static_cast<uint64_t>( bytes[i] );
    }
    return value;
}

DataValue *TimeStamp::readValue( std::ifstream &infile ) const
{
    long long sec;
    unsigned long long ns;
    std::array<uint8_t, 8> bytes;
    if (!infile.read( reinterpret_cast<char * >( bytes.data() ), bytes.size() ) )
    {
        std::cerr << "Failed to read 8 bytes." << std::endl;
        return nullptr;
    }
    uint64_t reversedValue = reverseBytes( bytes.data() );
    sec = static_cast<long long>( reversedValue );

    if (!infile.read( reinterpret_cast<char * >( bytes.data() ), bytes.size() ) )
    {
        std::cerr << "Failed to read 8 bytes." << std::endl;
        return nullptr;
    }
    reversedValue = reverseBytes( bytes.data() );
    ns = static_cast<unsigned long long>( reversedValue );

    return new StringValue( this, convert_to_timestamp( sec, ns ) );


//  long ns = (long) (Float.valueOf(str) / max) * 1000;
//  long ns = (long) (Float.valueOf(str) / max) ;
//  //System.out.println(ns);
//  long nanoseconds = ns; // 如果有分数秒，这里是纳秒部分
//  String resolution = "ns"; // 时间戳的分辨率
//  String resolution = "us"; // 时间戳的分辨率
//  //ZoneId zoneId = ZoneId.systemDefault(); // 系统默认时区
}

DataArray *TimeStamp::readArray( std::ifstream &infile, unsigned int size,
                                 unsigned int nbytes ) const
{
    int nByteNum = size * 16;
//    char *data = new char[nByteNum];
    long double *data = new long double[nByteNum];
    infile.read(( char* )data, nByteNum );
    return new TimeStampArray( this, data, size );
}

DataArray *TimeStamp::newArray( unsigned int size, unsigned int nbytes ) const
{
    char *data = new char[nbytes];
    return new StringArray( this, data, size, nbytes );
}

int TimeStamp::getTypeID() const
{
    return 68;
}
